package com.sinodata.bsm.cicp.finder.sybase;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.DBConnectionUtil;

/**
 * 
 * <p>
 * Description: Sybase设备发现程序
 * 程序返回：设备名称，设备类型，物理名称
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-15 上午09:45:52          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class Device extends Finder {

    private static final Logger logger = Logger.getLogger(Device.class);

    public Device() {
    }

    private String getDevTypeName(int type) {
        switch (type) {
            case 0:
                return "DataBase";
            case 2:
                //                return "磁盘转储设备或流式磁带";
                return "TapeDumpDevice or StreamingTape";
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
                //                return "磁带转储设备";
                return "TapeDumpDevice";
            default:
                return "UnKnown";
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String svrName = (String) params.get("SERVERNAME");
        String sql = "select name, cntrltype, phyname from master.dbo.sysdevices where cntrltype = 0 and (status & 2) = 2";
        Statement statement = null;
        ResultSet rs = null;
        Connection conn = null;
        try {
            conn = DBConnectionUtil.getInstance().getConnection("sybase", ip, svrName);
            if (conn == null) {
                logger.error("failed to connect sysbase server...");
                return list;
            }
        } catch (Exception e) {
            logger.error("failed to connect sysbase server", e);
        }
        String devName;
        String devType;
        String phyName;
        try {
            statement = conn.createStatement();
            statement.setQueryTimeout(10);
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                devName = rs.getString(1);
                devType = getDevTypeName(rs.getInt(2));
                phyName = rs.getString(3);
                String[] deviceInfo = new String[6];
                list.add(deviceInfo);
                deviceInfo[0] = devName;
                deviceInfo[1] = devName;
                deviceInfo[2] = devName;
                deviceInfo[3] = devName; //设备名称
                deviceInfo[4] = devType; //设备类型
                deviceInfo[5] = phyName; //物理名称
            }
        } catch (Exception e) {
            logger.error("failed to find sysbase device...", e);
        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (SQLException ex) {
                    logger.error("failed to close rst", ex);
                }
            if (statement != null)
                try {
                    statement.close();
                } catch (SQLException ex1) {
                    logger.error("failed to close statement", ex1);
                }
            DBConnectionUtil.getInstance().returnConnection(conn);
        }

        return list;
    }

}
